﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Chatclient;
using PluginInterface;

namespace MessageStoragePack
{
	public class MessageStorageFunctionality : IPlugin
	{
		IPluginHost myPluginHost = null;
		string myPluginName = "MessageStoragePack";
		string myPluginAuthor = "MessengerTeam";
		string myPluginDescription = "Fügt Funktionalität zum Nachrichten speichern hinzu.";
		string myPluginVersion = "1.0.0";

		public void addFunctionality(System.Windows.Forms.Form form)
		{
			// TODO: Form erweitern
			Console.WriteLine("(MessageStorageFunctionality) Form verändert: " + form);
			FormPrivateChat fpc = (FormPrivateChat)form;
			fpc.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormPrivateChat_FormClosing);

			Button btnHistory = new Button();
			btnHistory.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			btnHistory.Location = new System.Drawing.Point(190, 359);
			btnHistory.Name = "btnHistory";
			btnHistory.Size = new System.Drawing.Size(75, 23);
			btnHistory.TabIndex = 6;
			btnHistory.Text = "History";
			btnHistory.UseVisualStyleBackColor = true;
			btnHistory.Click += new System.EventHandler(this.btnHistory_Click);
			fpc.Controls.Add(btnHistory);

		}

		private void btnHistory_Click(object sender, EventArgs e)
		{
			Button sendButton = (Button)sender;
			FormPrivateChat f = (FormPrivateChat)sendButton.Parent;
			ShowHistory(f);
		}

		#region IPlugin
		public IPluginHost Host
		{
			get
			{
				return myPluginHost;
			}
			set
			{
				myPluginHost = value;
			}
		}

		public string Name
		{
			get { return myPluginName; }
		}

		public string Description
		{
			get { return myPluginDescription; }
		}

		public string Author
		{
			get { return myPluginAuthor; }
		}

		public string Version
		{
			get { return myPluginVersion; }
		}

		public void Initialize()
		{
			// 
		}

		public void Dispose()
		{
			// 
		}
		#endregion


		public void handleMessage(MessageHandler.Message message, object form)
		{
			if (form is string && message == null)
			{
				try
				{
					string newHistory = (string)form;
					string[] splitted = newHistory.Split('³');

					AddToHistory(splitted[0], splitted[1], splitted[2]);
				}
				catch
				{
				}
			}
		}

		#region History-Quatsch
		private void ShowHistory(FormPrivateChat f)
		{
			PrivateChatMembers pcm = f.ChatMembers;
			SaveHistory(f);
			new FormHistory(pcm).ShowDialog();
		}

		private void FormPrivateChat_FormClosing(object sender, FormClosingEventArgs e)
		{

			FormPrivateChat f = (FormPrivateChat)sender;
			SaveHistory(f);
		}

		private void SaveHistory(FormPrivateChat fpc)
		{
			String PersonalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			string directory = PersonalFolder + "/Messenger/history/" + fpc.userInfo;
			if (!Directory.Exists(directory))
			{
				Directory.CreateDirectory(directory);
			}
			File.AppendAllText(directory + "/" + fpc.ChatMembers.PrintUsers() + ".history", history.ToString());
			history.Clear();
		}

		StringBuilder history = new StringBuilder();
		private void AddToHistory(string head, string message, string dateTime)
		{
			history.AppendLine(dateTime + "_" + head + "_" + message);
		}
		#endregion
	}
}